package reflect;

import javax.annotation.processing.Filer;
import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * 自动调用与当前类Text03在同一个保重这些类的公开的 无参的
 * 名字里含有"s"的方法
 *
 */
public class Test03 {
    public static void main(String[] args) throws Exception {
        Class cls = Test03.class;
        System.out.println(cls);
        String name = cls.getPackage().getName();
        System.out.println(name);
        File dir = new File(cls.getResource(".").toURI());

        File [] subs = dir.listFiles(f->f.getName().endsWith(".class"));
        for (File sub :subs){
            String name1 = sub.getName();
            String classname = name1.substring(0,name1.indexOf("."));
            Class c = Class.forName(name+"."+classname);

            Method[] Methods = c.getDeclaredMethods();
            for (Method method : Methods){
               if (method.getModifiers()== Modifier.PUBLIC&&
                   method.getParameterCount()==0&&
                   method.getName().contains("s")
               ){
                   System.out.println("方法名"+method.getName());
                   Object o = c.newInstance();
                   method.invoke(o);
               }
            }

        }
    }
}
